
//网站名称
var webname = '佰の林';

//菜单列表路径 可以是本地json 也可以是api接口
var menuUrl = '/admin/js/menu_user.json';
/**
 * 判断是否为空
 * @param value
 * @returns {boolean}
 */
 let isEmpty =function (value) {
    if (typeof value === 'undefined') {
        return true;
    } else if (value === null) {
        return true;
    } else if (typeof value === 'string' && value === '') {
        return true;
    } else if (typeof value === 'number' && isNaN(value)) {
        return true;
    } else if (Array.isArray(value) && value.length === 0) {
        return true;
    } else if (value instanceof Number && isNaN(value)) {
        return true;
    } else if (
        value.toString() === '[object Object]' &&
        Object.keys(value).length === 0
    ) {
        return true;
    } else {
        return false;
    }
};

/**
 * 判断是否为不为空
 * @param value
 * @returns {boolean}
 */
let isNotEmpty = function (value) {
    return !isEmpty(value);
};

let vue = new Vue({
    el: '#fixedData',
    data: {
        webname: webname,
        menu: [],
        status: 0,
        active: 'active',
        address: [],
    },
    methods: {
        //记住收展
        /**
         * @param pid 左侧目录栏大表题索引
         * 点击那个标签，将当前标签展开，设置active尾true 其余设置为false
         */

        onActive: function (pid, id = false) {
            let data; //用来表示点击的那个大标题
            if (id === false) {
                data = this.menu[pid];
                if (data.url.length > 0) {
                    this.menu.forEach((v, k) => {
                        v.active = false;
                        v.list.forEach((v2, k2) => {
                            v2.active = false;
                        })
                    })
                    data.active = true;
                }
                data.hidden = !data.hidden;
                console.log(data,'1111data')

            } else {
                this.menu.forEach((v, k) => {
                    v.active = false;
                    v.list.forEach((v2, k2) => {
                        v2.active = false;
                    })
                })

                data = this.menu[pid].list[id];
            }
            this.updateStorage();
            if (data.url.length > 0) {
                window.location.href = data.url;
            }
        },

        //更新菜单缓存
        updateStorage() {
            localStorage.setItem('menu',JSON.stringify(this.menu))
            console.log(123321)
        },
        //菜单高亮,设置当前选中菜单
        thisActive: function () {
            let pathname = window.location.pathname;
            let host = window.location.host;
            console.log(window.location,'window.location')
            //为了获取路由包括参数
            let regPath = window.location.href.replace(window.location.origin,'')
            let pid = false;
            let id = false;
            this.menu.forEach((v, k) => {
                let url = v.url;
                if (pathname === url) {
                    pid = k;
                }
                v.list.forEach((v2, k2) => {
                    let url = v2.url;
                    //当路由等于点击的按钮时
                    if (pathname === url || regPath === url ) {
                        pid = k;
                        id = k2;
                    }
                })
            })


            if (id !== false) {
                this.menu[pid].list[id].active = true;
            } else {
                if (pid !== false) {
                    this.menu[pid].active = true;
                }
            }

            this.updateStorage();

        },
        //当前位置(设置面包屑导航)
        thisAttr: function () {
            // 当前位置
            let address = [{
                name: '首页',
                url: '/',
            }];
            this.menu.forEach((v, k) => {
                v.list.forEach((v2, k2) => {
                    if (v2.active) {
                        address.push({
                            name: v.name,
                            url: 'javascript:;'
                        })
                        address.push({
                            name: v2.name,
                            url: v2.url,
                        })
                        this.address = address;
                    }
                })
            })
            this.updateStorage();
        },
    },
    mounted() {
        //加载左侧菜单
        let data = sessionStorage.getItem('menu');
        console.log(JSON.parse(data),'123true')
        if (isEmpty(data)) {
            $.ajax({
                url: menuUrl,
                dataType: 'text',
                success: (menu) => {
                    menu = eval('(' + menu + ')');
                    sessionStorage.menu = JSON.stringify(menu);
                    this.menu = menu;
                    this.thisActive();
                    this.thisAttr();
                    this.updateStorage();
                    console.log(this.menu,'this.menu')
                }
            })
        } else {
            this.menu = JSON.parse(data);
            this.thisActive();
            this.thisAttr();
            this.updateStorage();
            console.log(JSON.parse(data),'data')
        }


    },
})